﻿#include "fowidget.h"
#include "ui_fowidget.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QTextDocument>
#include <QTextBlock>
int FOwidget::identification=0;
FOwidget::FOwidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FOwidget)
{
    ui->setupUi(this);
    FileOut();
}

FOwidget::~FOwidget()
{
    delete ui;}

void FOwidget::FileOut()
{
    ui->tbWdgtOut->setSelectionBehavior(QAbstractItemView::SelectRows);  //整行选中的方式
    ui->tbWdgtOut->setEditTriggers(QAbstractItemView::NoEditTriggers);//不可修改
    ui->dtEdtOutStart->setDate(QDate::currentDate());
    ui->dtEdtOutEnd->setDate(QDate::currentDate());
    ui->tbWdgtOut->setColumnHidden(0, true);//隐藏首列id
    ui->tbWdgtOut->horizontalHeader()->setResizeMode(QHeaderView::Stretch); //自适应列宽
    connect(ui->tbWdgtOut->horizontalHeader(),SIGNAL(sectionClicked(int)),this,SLOT(FOshowsort(int)));
    QSqlQuery query;
    QString strSelect=QString("SELECT * FROM FileOut limit 20;");
    query.exec(strSelect);
    while (query.next()) {
        ui->tbWdgtOut->insertRow(0);
        for(int a=0 ; a<10 ; a++){
                if(a == 3){
                    switch (query.value(a).toInt()) {
                    case 1:
                        ui->tbWdgtOut->setItem(0,a,new QTableWidgetItem("居民身份证"));
                        break;
                    case 2:
                        ui->tbWdgtOut->setItem(0,a,new QTableWidgetItem("军官证"));
                        break;
                    case 3:
                        ui->tbWdgtOut->setItem(0,a,new QTableWidgetItem("士官证"));
                        break;
                    case 4:
                        ui->tbWdgtOut->setItem(0,a,new QTableWidgetItem("义务兵证"));
                        break;
                    case 5:
                        ui->tbWdgtOut->setItem(0,a,new QTableWidgetItem("港澳台居民居住证"));
                        break;
                    case 6:
                        ui->tbWdgtOut->setItem(0,a,new QTableWidgetItem("外籍人士在我国永久居住证"));
                        break;
                    case 7:
                        ui->tbWdgtOut->setItem(0,a,new QTableWidgetItem("护照"));
                        break;
                    case 9:
                        ui->tbWdgtOut->setItem(0,a,new QTableWidgetItem("其他"));
                        break;
                    default:
                        break;
                    }
                }else{
                    ui->tbWdgtOut->setItem(0,a,new QTableWidgetItem(query.value(a).toString()));
                }
        }
    }
}

void FOwidget::FOshow()
{
    ui->tbWdgtOut->clearContents();
    ui->tbWdgtOut->setRowCount(0);
    QSqlQuery query;
    QString strSelect=QString("SELECT * FROM FileOut;");
    query.exec(strSelect);
    while (query.next()) {
        ui->tbWdgtOut->insertRow(0);
        for(int a=0 ; a<10 ; a++){
                if(a == 3){
                    switch (query.value(a).toInt()) {
                    case 1:
                        ui->tbWdgtOut->setItem(0,a,new QTableWidgetItem("居民身份证"));
                        break;
                    case 2:
                        ui->tbWdgtOut->setItem(0,a,new QTableWidgetItem("军官证"));
                        break;
                    case 3:
                        ui->tbWdgtOut->setItem(0,a,new QTableWidgetItem("士官证"));
                        break;
                    case 4:
                        ui->tbWdgtOut->setItem(0,a,new QTableWidgetItem("义务兵证"));
                        break;
                    case 5:
                        ui->tbWdgtOut->setItem(0,a,new QTableWidgetItem("港澳台居民居住证"));
                        break;
                    case 6:
                        ui->tbWdgtOut->setItem(0,a,new QTableWidgetItem("外籍人士在我国永久居住证"));
                        break;
                    case 7:
                        ui->tbWdgtOut->setItem(0,a,new QTableWidgetItem("护照"));
                        break;
                    case 9:
                        ui->tbWdgtOut->setItem(0,a,new QTableWidgetItem("其他"));
                        break;
                    default:
                        break;
                    }
                }else{
                    ui->tbWdgtOut->setItem(0,a,new QTableWidgetItem(query.value(a).toString()));
                }
        }
    }
}

void FOwidget::on_chkBxOutStart_clicked(bool checked)
{
    ui->dtEdtOutStart->setEnabled(checked);
}

void FOwidget::on_chkBxOutEnd_clicked(bool checked)
{
    ui->dtEdtOutEnd->setEnabled(checked);
}

void FOwidget::on_FOadd_clicked()
{
    FOaddDialog foadd(this);
    foadd.exec();
}

void FOwidget::on_FOdelete_clicked()
{
    QList<QTableWidgetItem*> itemsTemp = ui->tbWdgtOut->selectedItems();
    int countTemp = itemsTemp.count();
    if(countTemp == 0){//如果未选中记录
        QMessageBox *msgBox = new QMessageBox(QMessageBox::Warning,tr("提示"),tr("请先选中要删除的信息！"), QMessageBox::Abort);
        msgBox->button(QMessageBox::Abort)->setText("确定");
        msgBox->exec();
    }else{
        ui->tbWdgtOut->showColumn(0);//取id前使隐藏列可见
        QList<QTableWidgetSelectionRange> ranges = ui->tbWdgtOut->selectedRanges();
        QStringList list;//存放id
        int countRow = ranges.count();
        for(int i=0 ; i < countRow ; i++)
        {
           int topRow=ranges.at(i).topRow();
           int bottomRow=ranges.at(i).bottomRow();
           for(int j=topRow;j<=bottomRow;j++)
           {
              list.append(ui->tbWdgtOut->item(j,0)->text());
            }
        }
        ui->tbWdgtOut->setColumnHidden(0, true);//取完id后继续隐藏id
        QSqlQuery query;
        QMessageBox::StandardButton btnDel;
        btnDel = QMessageBox::question(this, "提示", "确定删除吗?", QMessageBox::Yes|QMessageBox::No);
        if (btnDel == QMessageBox::Yes) {
            for(int i = 0; i< list.size();i++)
            {
                QString strItem = list.at(i);
                QString strSql = QString("DELETE FROM FileOut WHERE FileOutID='%1';").arg(strItem);
                qDebug()<<strSql;
                bool delFlag = query.exec(strSql);
                if(delFlag == false){
                    QMessageBox::information(this,"提示信息","删除失败!");
                }
            }
            on_FOseek_clicked();
            ui->tbWdgtOut->selectRow(-1);
        }
    }
}

void FOwidget::on_FOseek_clicked()
{
    QString strToUnit = ui->edtToUnit->text().trimmed();
    QString strName = ui->edtName->text().trimmed();
    QString strPreDuty = ui->edtPreDuty->text().trimmed();
    QString strOutReason = ui->edtOutReason->text().trimmed();
    QString strDuringPeople = ui->edtDuringPeople->text().trimmed();
    QString strRemark = ui->edtRemark->text().trimmed();
    QString strTimeStart = ui->dtEdtOutStart->date().toString("yyyy-MM-dd");
    QString strTimeEnd = ui->dtEdtOutEnd->date().toString("yyyy-MM-dd");
    QString strSql = QString("SELECT * FROM FileOut WHERE 1=1 ");
    if(!strToUnit.isEmpty()){
        strSql += QString("AND FileToUnit = '%1' ").arg(strToUnit);
    }
    if(!strName.isEmpty()){
        strSql += QString("AND FilePerName = '%1' ").arg(strName);
    }
    if(!strPreDuty.isEmpty()){
        strSql += QString("AND OrignalDuty = '%1' ").arg(strPreDuty);
    }
    if(!strOutReason.isEmpty()){
        strSql += QString("AND FileOutRsn LIKE '%%1%'").arg(strOutReason);
    }
    if(!strDuringPeople.isEmpty()){
        strSql += QString("AND FileDrgName = '%1'").arg(strDuringPeople);
    }if(!strRemark.isEmpty()){
        strSql += QString("AND FileOutRmk LIKE '%%1%'").arg(strRemark);
    }
    if(ui->chkBxOutStart->isChecked()){
        strSql += QString("AND FileOutTime >= '%1'").arg(strTimeStart);
    }
    if(ui->chkBxOutEnd->isChecked()){
        strSql += QString("AND FileOutTime <= '%1'").arg(strTimeEnd);
    }
    QSqlQuery query;
    query.exec(strSql);
    ui->tbWdgtOut->clearContents();
    ui->tbWdgtOut->setRowCount(0);
    while (query.next()) {
        ui->tbWdgtOut->insertRow(0);
        for(int a=0 ; a<10 ; a++){
            if(a == 3){//判断证件类型
                int temp = query.value(a).toInt();
                switch (temp) {
                case 1:
                    ui->tbWdgtOut->setItem(0,a,new QTableWidgetItem(QString("居民身份证")));
                    break;
                case 2:
                    ui->tbWdgtOut->setItem(0,a,new QTableWidgetItem(QString("军官证")));
                    break;
                case 3:
                    ui->tbWdgtOut->setItem(0,a,new QTableWidgetItem(QString("士官证")));
                    break;
                case 4:
                    ui->tbWdgtOut->setItem(0,a,new QTableWidgetItem(QString("义务兵证")));
                    break;
                case 5:
                    ui->tbWdgtOut->setItem(0,a,new QTableWidgetItem(QString("港澳台居民居住证")));
                    break;
                case 6:
                    ui->tbWdgtOut->setItem(0,a,new QTableWidgetItem(QString("外籍人士在我国永久居住证")));
                    break;
                case 7:
                    ui->tbWdgtOut->setItem(0,a,new QTableWidgetItem(QString("护照")));
                    break;
                case 9:
                    ui->tbWdgtOut->setItem(0,a,new QTableWidgetItem(QString("其他")));
                    break;
                default:
                    break;
                }
            }else{
                ui->tbWdgtOut->setItem(0,a,new QTableWidgetItem(query.value(a).toString()));
            }
        }
    }
}

void FOwidget::on_FOedit_clicked()
{
    int i=ui->tbWdgtOut->currentRow();
    if(i>-1){
        int fileOutID = ui->tbWdgtOut->item(i,0)->text().toInt();//获得单ID
        FOeditDialog foedit(this);
        connect(this,SIGNAL(sendDa(int)),&foedit,SLOT(receiveID(int)));
        emit sendDa(fileOutID);
        foedit.exec();
        on_FOseek_clicked();
    }
    else{
    QMessageBox::information(this,"提示信息","未选中编辑行!");
    }
}

void FOwidget::on_FOPDF_clicked()
{
    int i=ui->tbWdgtOut->currentRow();
    if(i>-1){
        QString filePth = QCoreApplication::applicationDirPath() + "/";
        QString fileName = QFileDialog::getSaveFileName(this, tr("导出PDF文件"), filePth, "*.pdf");
        if (!fileName.isEmpty())
        {
            // 如果文件后缀为空，则默认使用.pdf
            if (QFileInfo(fileName).suffix().isEmpty())
            {
                fileName.append(".pdf");
            }
            QPrinter *printer = new QPrinter(QPrinter::ScreenResolution);
            // 指定输出格式为pdf
            printer->setPageSize(QPrinter::A4);
            printer->setOutputFormat(QPrinter::PdfFormat);
            printer->setOutputFileName(fileName);
            QTextDocument text_document;
            text_document.setHtml(saveHtmlToPDF(fileName));
            text_document.print(printer);
            text_document.end();
        }
    }else{
        QMessageBox::information(this,"提示信息","未选择打印条目行!");
    }
}

QString FOwidget::saveHtmlToPDF(QString Q)
{
    QString html = "";
    html += "<html>";
    html += "<head>";
    html += "<title>"+Q+"</title>";
    html += "<head>";
    html += "<body>";
    html += "<font size=\"4\" face=\"arial\">";
    html += "<table width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"10\" align=\"center\">";
    html += "<tr>";
        html += "<th colspan=\"4\">档案转出表</th>";
    html += "</tr>";
    QList<QTableWidgetSelectionRange> ranges = ui->tbWdgtOut->selectedRanges();
    int countTemp = ranges.count();
    for(int i=0;i<countTemp;i++)
    {
        int topRow=ranges.at(i).topRow();
        int bottomRow=ranges.at(i).bottomRow();
        for(int j=topRow;j<=bottomRow;j++)
        {
        html += "<tr>";
            html += "<td align=\"center\">转出时间：</td>";
            html += "<td align=\"center\">"+ui->tbWdgtOut->item(j,1)->text()+"</td>";
            html += "<td align=\"center\">转去单位：</td>";
            html += "<td align=\"center\">"+ui->tbWdgtOut->item(j,6)->text()+"</td>";
        html += "</tr>";
        html += "<tr>";
            html += "<td align=\"center\" >姓名：</td>";
            html += "<td align=\"center\">"+ui->tbWdgtOut->item(j,2)->text()+"</td>";
            html += "<td align=\"center\">原单位职务：</td>";
            html += "<td align=\"center\">"+ui->tbWdgtOut->item(j,5)->text()+"</td>";
        html += "</tr>";
        html += "<tr>";
            html += "<td align=\"center\">"+ui->tbWdgtOut->item(j,3)->text()+"：</td>";
            html += "<td align=\"center\">"+ui->tbWdgtOut->item(j,4)->text()+"</td>";
            html += "<td align=\"center\" >经办人：</td>";
            html += "<td align=\"center\" >"+ui->tbWdgtOut->item(j,8)->text()+"</td>";
        html += "</tr>";
        html += "<tr>";
            html += "<td align=\"center\">转出原因：</td>";
            html += "<td align=\"center\" colspan=\"3\">"+ui->tbWdgtOut->item(j,7)->text()+"</td>";
        html += "</tr>";
        html += "<tr>";
            html += "<td align=\"center\">备注：</td>";
            html += "<td align=\"center\" colspan=\"3\">"+ui->tbWdgtOut->item(j,9)->text()+"</td>";
        html += "</tr>";
        }}
    html += "</table>";
    html += "</font>";
    html += "</body>";
    html += "< / html>";
    return html;
}

void FOwidget::FOshowsort(int clounm)
{
    if(identification==0){
    ui->tbWdgtOut->sortItems(clounm,Qt::DescendingOrder);
    identification=1;}
    else{ui->tbWdgtOut->sortItems(clounm,Qt::AscendingOrder);
    identification=0;}
}
